﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习九月份
{
    public class Solution112
    {
        public bool HasPathSum(TreeNode root, int targetSum)
        {
            if (root == null)
            {
                return false;
            }

            return DFS(root, 0, targetSum);
        }

        private bool DFS(TreeNode root, int curSum, int targetSum)
        {
            curSum += root.val;

            if (root.left == null && root.right == null)
            {
                //遇到叶子节点，判断，并收集
                if (curSum == targetSum)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            //收集当前顶点
            bool left = false;
            bool right = false;
            if (root.left != null)
            {
                left = DFS(root.left, curSum, targetSum);
            }
            if (root.right != null)
            {
                right = DFS(root.right, curSum, targetSum);
            }
            return left || right;
        }
    }
}